<template>
  <div>
    <div class="com-top-nav">
      <van-nav-bar
        title="报名列表"
        left-arrow
        @click-left="clickhandle"
      />
    </div>
    <div v-for="(item, i ) in info" :key="i" list>
      <div class="item">
        <router-link :to="{path: 'personInfo', query:{uid: item.uid}}"><img :src="item.headimgurl" alt=""></router-link>
        <div>
          <p>{{item.user_name?item.user_name:item.nickname}}</p>
          <span>信誉：98分</span>
        </div>
      </div>
      <van-button size="normal" @click="pay(item.uid,item.taskid)">把任务给TA</van-button>
    </div>
  </div>
</template>
<script>
  export default {
    data(){
      return{
        info:''
      }
    },
    mounted(){
      this.checkPay()

    },
    methods:{
      clickhandle(){
        this.$router.go(-1)
      },
      /*
       * 支付
       * @param uid
       * @param taskid
       */
      pay(uid,taskid){

        let out_trade_no='2018'+Math.random().toString().substr(2, 10);
        let sion = {
          t_uid:uid,
          uid: JSON.parse(sessionStorage.getItem('user')).uid,
          out_trade_no:out_trade_no,
          taskid:this.$route.query.taskid,
          openid:JSON.parse(sessionStorage.getItem('user')).openid,
        }
        sessionStorage.setItem('sion',JSON.stringify(sion))
        this.axios.get('/api/wxpay',{params:{
            uid: JSON.parse(sessionStorage.getItem('user')).uid,
            taskid:taskid,
            body:'文字',
            out_trade_no:out_trade_no,
            fee:1,
            openid:JSON.parse(sessionStorage.getItem('user')).openid
          }}).then(result=>{
          function onBridgeReady() {
            window.WeixinJSBridge.invoke(
              "getBrandWCPayRequest",
              {
                appId: result.data.appId,
                timeStamp: result.data.timeStamp.toString(),
                nonceStr: result.data.nonceStr,
                package: result.data.package,
                signType: result.data.signType,
                paySign: result.data.paySign
              },

              function(res) {
                if (res.err_msg == "get_brand_wcpay_request:ok") {
                  // 使用以上方式判断前端返回,微信团队郑重提示：
                  //res.err_msg将在用户支付成功后返回ok，但并不保证它绝对可靠。
                    let sion =JSON.parse(sessionStorage.getItem('sion'))
                    window.location.href=window.location.href +`&t_uid=${sion.uid}&uid=${sion.t_uid}&out_trade_no=${sion.out_trade_no}&openid=${JSON.parse(sessionStorage.getItem('user')).openid}`
                  // this.$router.replace({name:'registrationList',query:sion})


                } else {
                }
              }
            );
          }
          if (typeof WeixinJSBridge == "undefined") {
            if (document.addEventListener) {
              document.addEventListener(
                "WeixinJSBridgeReady",
                onBridgeReady,
                false
              );
            } else if (document.attachEvent) {
              document.attachEvent("WeixinJSBridgeReady", onBridgeReady);
              document.attachEvent("onWeixinJSBridgeReady", onBridgeReady);
            }
          } else {
            onBridgeReady();
          }
        })
      },

      /**
       * 支付结果查询
       */
      checkPay(){

        let sion;
        if(this.$route.query.openid){
            if(sessionStorage.getItem('sion')){sessionStorage.removeItem('sion')}
           sion = JSON.parse(sessionStorage.getItem('sion'))
        }else{
          return;
        }
        this.axios.get('/api/wxpay/result',{
          params:{
            out_trade_no:this.$route.query.out_trade_no,
            taskid:this.$route.query.taskid,
            uid:this.$route.query.uid
          }
        }).then(re=>{
          let t_uid = JSON.parse(sessionStorage.getItem('user')).uid;
          let uid = this.$route.query.uid;
          let taskid = this.$route.query.taskid;
          let params = {
            t_uid,uid,taskid
          }
          this.axios.get('/api/task_auth',{params}).then(res=>{

            if(res.data.err_code == 0){
              this.$toast({message:res.data.data})
              this.$router.push({name:'job'});
            }else{
              this.$toast({message:'出错了'})
            }
          }).catch(e=>{
            console.log(e)

          })

        })
      }
    },
    created(){
      this.userId = JSON.parse(sessionStorage.getItem('user')).uid;
      this.axios.get('/api/task_enrol_list',{params:{uid:JSON.parse(sessionStorage.getItem('user')).uid,taskid:this.$route.query.taskid}}).then(res=>{
        if(res.data.err_code == 0){
          this.info = res.data.data;
        }
      }).catch(e=>{
        console.log(e)
      })
    }
  }
</script>
<style lang="less" scoped>
  [list]{
    background: #ffffff;
    border-bottom: 1px solid #eee;
    padding: 10px;
    display: flex;justify-content: space-between;align-items: center;
    .item{
      display: flex;
      align-items: center;
      div{
        p{
          font-size: 16px;
          color: #333;
        }
        span{
          display: block;
          background: #00baad;
          font-size: 10px;
          padding: 2px 0px;
          text-align: center;
          border-radius: 3px;
          color: #fff;
        }
      }
    }
    img{
      height: 64px;
      width: 64px;
      margin-right: 10px;
    }
  }
</style>

